

package AddressBook.View.UtilityView;

import AddressBook.Controller.SwingController;
import AddressBook.Model.Contact;
import AddressBook.Model.Exception.KeyAlreadyUsedHashMapException;
import AddressBook.Model.Exception.NoSuchKeyFoundHashMapException;
import AddressBook.Model.Group;
import AddressBook.Model.GroupManager;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;

/**
 * The Class JtableModelItem provided a specified jtable model to display the item
 */
public class JtableModelItem extends AbstractTableModel {

    /**
     * This field stores the list of items
     */
    //private ArrayList<Object[]> objectList = new ArrayList<Object[]>();
    private ArrayList<Contact> contactList = new ArrayList<Contact>();
    private GroupManager groupManager;
    private SwingController controller;

    public void setController(SwingController controller) {
        this.controller = controller;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        super.setValueAt(aValue, rowIndex, columnIndex);    //To change body of overridden methods use File | Settings | File Templates.

        Object data = contactList.get(rowIndex);

        Contact c = (Contact) data;
        String value = (String) aValue;

        switch (columnIndex) {
            case 0:
                c.setFirstName(value);
                break;
            case 1:
                c.setLastName(value);
                break;
            case 2:

                try {
                    Group g = groupManager.getGroup(c.getGroupName());
                    g.removeContact(c);

                    g = groupManager.getGroup(value);
                    g.addContact(c);

                    c.setGroup(value);

                } catch (NoSuchKeyFoundHashMapException e) {
                    try {
                        c.setGroup(value);
                        groupManager.addContact(c);
                        controller.initGroupSelector();
                    } catch (KeyAlreadyUsedHashMapException e1) {
                        e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                } catch (KeyAlreadyUsedHashMapException e) {
                    e.printStackTrace();
                }

                break;
            case 3:
                c.setHomeAddress(value);
                break;
            case 4:
                c.setCompagnyAddress(value);
                break;
            case 5:
                c.setPersonalPhone(value);
                break;
            case 6:
                c.setProfessionalPhone(value);
                break;
            case 7:
                c.setHomePage(value);
                break;
            case 8:
                c.setEmail(value);
                break;
            case 9:
                c.setUriPicture(value);
                break;

        }
    }

    public void setGroupManager(GroupManager groupManager) {
        this.groupManager = groupManager;
    }

    /**
     * This field stores the header of the table
     */
    private String[] headers = {"FirstName", "LastName", "Group", "HomeAdr", "CompanyAdr", "PersonalPhone", "ProfessionalPhone", "HomePage", "Email", "Photo"};

    /**
     * This method returns the class type of the specified column
     *
     * @param columnIndex The column index
     * @return The class type
     */
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        /*switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return String.class;
            case 2:
                return String.class;
            case 3:
                return Integer.class;
            case 4:
                return Integer.class;
            case 5:
                return String.class;
            case 6:
                return String.class;
        } */
        return String.class;
    }

    /**
     * This method returns true if cell is editable, false otherwise
     *
     * @param rowIndex    The row index
     * @param columnIndex The column index
     * @return True if editable, false otherwise
     */
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {

        return true;
    }

    /**
     * This method returns the columnName of a specified column
     *
     * @param column The desired column
     * @return The name of teh column
     */
    @Override
    public String getColumnName(int column) {
        return headers[column];
    }

    /**
     * This method returns the number of rows
     *
     * @return The number of rows
     */
    @Override
    public int getRowCount() {

        return contactList.size();  //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * This method returns the number of columns
     *
     * @return The number of columns
     */
    @Override
    public int getColumnCount() {
        return headers.length;  //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * This method returns the value at the specified position
     *
     * @param rowIndex    The desired row
     * @param columnIndex The desired column
     * @return The value at the specified position
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        switch (columnIndex) {
            case 0:
                return contactList.get(rowIndex).getFirstName();

            case 1:
                return contactList.get(rowIndex).getLastName();

            case 2:
                return contactList.get(rowIndex).getGroupName();

            case 3:
                return contactList.get(rowIndex).getHomeAddress();

            case 4:
                return contactList.get(rowIndex).getCompagnyAddress();

            case 5:
                return contactList.get(rowIndex).getPersonalPhone();

            case 6:
                return contactList.get(rowIndex).getProfessionalPhone();

            case 7:
                return contactList.get(rowIndex).getHomePage();

            case 8:
                return contactList.get(rowIndex).getEmail();

            case 9:
                return contactList.get(rowIndex).getUriPicture();

            default:
                return contactList.get(rowIndex);

        }

    }

    /**
     * This method allows to add item to the table
     *
     * @param i The desired item
     */
    public void addItem(Contact i) {

        /*Object[] obj = new Object[headers.length];
        obj[0] = i.getFirstName();
        obj[1] = i.getLastName();
        obj[2] = i.getGroupName();
        obj[3] = i.getHomeAddress();
        obj[4] = i.getCompagnyAddress();
        obj[5] = i.getPersonalPhone();
        obj[6] = i.getProfessionalPhone();
        obj[7] = i.getHomePage();
        obj[8] = i.getEmail();
        obj[9] = i.getUriPicture();

        objectList.add(obj);   */

        contactList.add(i);
        fireTableDataChanged();

    }
}
